<?php
declare (strict_types=1);

namespace app\admin\service;

use app\admin\model\MarketSocialMediaAccountGroup as MarketSocialMediaAccountGroupModel;
use app\admin\model\SystemSmChannel as SystemSmChannelModel;

class MarketSocialMediaAccountGroup extends BaseService
{
    /**
     * 注册服务
     *
     * @return mixed
     */
    public function register()
    {
        //
    }

    /**
     * 执行服务
     *
     * @return mixed
     */
    public function boot()
    {
        //
    }

    /**
     * 分页列表
     * @param $param
     * @param $page
     * @param $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 2024-10-28 19:01:29
     * author: jiang
     */
    public function index($param, $page, $limit)
    {
        $arrData = ['code' => 0, 'message' => 'success'];
        $where = [['is_del', '=', 0]];
        if ($param['name']) {
            $where[] = ['name', 'like', '%' . $param['name'] . '%'];
        }
        if ($param['channel_id']) {
            $where[] = ['channel_id', '=', $param['channel_id']];
        }

        $oMarketSocialMediaAccountGroupModel = MarketSocialMediaAccountGroupModel::with(['get_creator_name', 'get_update_name'])->where($where);
        $result = $oMarketSocialMediaAccountGroupModel->page($page, $limit)->order('id', 'desc')->select();
        $total = $oMarketSocialMediaAccountGroupModel->count();
        foreach ($result as &$value) {
            $value->creator = $value->creator_value;
            unset($value->creator_value);
            $value->updater = $value->updater_value;
            unset($value->updater_value);
        }
        $arrData['data'] = ['list' => $result->toArray(), 'total' => $total];
        return $arrData;
    }

    /**
     * 新增
     * @param $param
     * @return array
     * 2024-10-28 19:08:00
     * author: jiang
     */
    public function save($param)
    {
        // 判断账号是否重复
        $count = MarketSocialMediaAccountGroupModel::where([['name', '=', $param['name']], ['is_del', '=', 0]])->count();
        if ($count > 0) {
            return ['code' => 1, 'message' => '分组名称重复！'];
        }
        $oMarketSocialMediaAccountGroupModel = new MarketSocialMediaAccountGroupModel();
        $oMarketSocialMediaAccountGroupModel->channel_id = $param['channel_id'];
        $oMarketSocialMediaAccountGroupModel->name = $param['name'];
        $oMarketSocialMediaAccountGroupModel->remark = $param['remark'];
        $oMarketSocialMediaAccountGroupModel->status = $param['status'];

        $oMarketSocialMediaAccountGroupModel->is_del = 0;
        $oMarketSocialMediaAccountGroupModel->creator = $param['operator_id'];
        $oMarketSocialMediaAccountGroupModel->create_time = date('Y-m-d H:i:s');
        if (!$oMarketSocialMediaAccountGroupModel->save()) {
            return ['code' => 1, 'message' => '添加失败！'];
        }
        $this->logger->logOperation($oMarketSocialMediaAccountGroupModel->id,'MarketSocialMediaAccountGroup','新增','新增社媒账号分组');
        return ['code' => 0, 'message' => '添加成功！'];
    }

    /**
     * 修改
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 2024-10-28 19:13:29
     * author: jiang
     */
    public function update($param)
    {
        $oMarketSocialMediaAccountGroupModel = MarketSocialMediaAccountGroupModel::find($param['id']);
        if (!$oMarketSocialMediaAccountGroupModel) {
            return ['code' => 1, 'message' => '数据不存在！'];
        }
        // 判断name是否重复
        $count = MarketSocialMediaAccountGroupModel::where([['name', '=', $param['name']], ['is_del', '=', 0]])->where('id', '<>', $param['id'])->count();
        if ($count > 0) {
            return ['code' => 1, 'message' => '分组名称重复！'];
        }

        $oSystemSmChannelModel = SystemSmChannelModel::find($param['channel_id']);
        if (!$oSystemSmChannelModel) {
            return ['code' => 1, 'message' => '渠道不存在！'];
        }

        $oMarketSocialMediaAccountGroupModel->channel_id = $param['channel_id'];
        $oMarketSocialMediaAccountGroupModel->name = $param['name'];
        $oMarketSocialMediaAccountGroupModel->remark = $param['remark'];
        $oMarketSocialMediaAccountGroupModel->status = $param['status'];

        $oMarketSocialMediaAccountGroupModel->updater = $param['operator_id'];
        $oMarketSocialMediaAccountGroupModel->update_time = date('Y-m-d H:i:s');
        if (!$oMarketSocialMediaAccountGroupModel->save()) {
            return ['code' => 1, 'message' => '修改失败！'];
        }
        $this->logger->logOperation($oMarketSocialMediaAccountGroupModel->id,'MarketSocialMediaAccountGroup','修改','修改社媒账号分组');
        return ['code' => 0, 'message' => '修改成功！'];
    }

    /**
     * 删除
     * @param $param
     * @return array
     * 2024-10-28 19:15:52
     * author: jiang
     */
    public function delete($param)
    {
        $result = MarketSocialMediaAccountGroupModel::whereIn('id', $param['ids'])->update([
            'is_del' => time(),
            'update_time' => date('Y-m-d H:i:s'),
            'updater' => $param['operator_id']
        ]);
        if (!$result) {
            return ['code' => 1, 'message' => '删除失败！'];
        }
        $this->logger->logOperation($param['ids'],'MarketSocialMediaAccountGroup','删除','删除社媒账号分组');
        return ['code' => 0, 'message' => '删除成功！'];
    }

    /**
     * 批量修改状态
     * @param $param
     * @return array
     * 2024-10-28 19:18:19
     * author: jiang
     */
    public function status($param)
    {
        $result = MarketSocialMediaAccountGroupModel::whereIn('id', $param['ids'])->update([
            'status' => $param['status'],
            'update_time' => date('Y-m-d H:i:s'),
            'updater' => $param['operator_id']
        ]);
        if (!$result) {
            return ['code' => 1, 'message' => '操作失败！'];
        }
        $this->logger->logOperation($param['ids'],'MarketSocialMediaAccountGroup','批量修改状态','批量修改社媒账号分组状态');
        return ['code' => 0, 'message' => '操作成功！'];
    }
}
